package org.dromara.third.convert.blindDiskWork.validator;

import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.blindDiskWork.dto.BlindDiskWorkDTO;
import org.dromara.third.convert.util.ExistenceValidator;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.Objects;

public class BlindDiskWorkValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return BlindDiskWorkDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        BlindDiskWorkDTO blindDiskWorkDTO = (BlindDiskWorkDTO) target;

        // 基础字段验证
        validateRequiredFields(blindDiskWorkDTO, errors);

        // 条件验证
        validateConditionalFields(blindDiskWorkDTO, errors);

        // 存在性验证
        validateExistence(blindDiskWorkDTO, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(BlindDiskWorkDTO dto, Errors errors) {
        ValidateUtil.rejectIfNull(dto::getZyid, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业ID不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getZyzbh, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业证编号不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getSqdw, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业申请单位不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getZysqsj, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业申请时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getSqr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业申请人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getZynr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业内容不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getZylb, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业类别不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getZywz, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业位置不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getZydwlx, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业单位类型不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getZydwmc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业单位名称不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getZyfzr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业负责人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getFxbs, String.valueOf(ExceptionEnum.AuthException01.getCode()), "风险辨识不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getDqzt, String.valueOf(ExceptionEnum.AuthException01.getCode()), "当前状态不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getStartTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "计划开始时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getEndTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "计划结束时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getSsbm, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属部门不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
    }

    /**
     * 验证条件字段
     */
    private void validateConditionalFields(BlindDiskWorkDTO dto, Errors errors) {
        // 作业状态不为作业前，以下字段必填
        if (!Objects.equals("1", dto.getDqzt()) && Objects.nonNull(dto.getDqzt())) {
            ValidateUtil.rejectIfEmpty(dto::getGdjz, String.valueOf(ExceptionEnum.AuthException01.getCode()), "管道介质不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getGdwd, String.valueOf(ExceptionEnum.AuthException01.getCode()), "管道温度不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getGdyl, String.valueOf(ExceptionEnum.AuthException01.getCode()), "管道压力不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getMbcz, String.valueOf(ExceptionEnum.AuthException01.getCode()), "盲板材质不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getMbgg, String.valueOf(ExceptionEnum.AuthException01.getCode()), "盲板规格不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getZyryxx, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业人员信息不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getZysskssj, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业实施开始时间不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getZyssjssj, String.valueOf(ExceptionEnum.AuthException01.getCode()), "作业实施结束时间不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getMbwztbh, String.valueOf(ExceptionEnum.AuthException01.getCode()), "盲板位置图编号不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getMbwzt, String.valueOf(ExceptionEnum.AuthException01.getCode()), "盲板位置图不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getBzrmc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "编制人名称不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getBzsj, String.valueOf(ExceptionEnum.AuthException01.getCode()), "编制时间不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSfsjqttszy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "是否涉及的其他特殊作业不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSjqttszyzbh, String.valueOf(ExceptionEnum.AuthException01.getCode()), "涉及其他特殊作业的作业证编号不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getAqjdr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "安全交底人不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getJsjdr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "接受交底人不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getJhr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "监护人不能为空", errors);
        }
    }

    /**
     * 验证存在性
     */
    private void validateExistence(BlindDiskWorkDTO dto, Errors errors) {
        // 验证所属部门存在
        ExistenceValidator.validateDeptExists(dto.getSsbm(), "所属部门", errors);

        // 验证创建人存在
        ExistenceValidator.validateUserExists(dto.getCreateBy(), "创建人", errors);

        // 验证更新人存在
        ExistenceValidator.validateUserExists(dto.getUpdateBy(), "更新人", errors);
    }
}
